import { astar } from "./astar";
import { getImage, RenderableBehaviour, SerilizeField, propertyEditor_None, core, Transform } from "./gameengine";
import { createTileMapItemPrefab } from "./data";


export const propertyEditor_TileMap = (host, script, key) => {
    const value = script[key][0];
    console.log(value)

    let length = script[key.length];
    const obj: any = {};

    for (let i = 0; i < length; i++) {
        let lineKey = 'line' + (i + 1)
        obj[lineKey] = script[key][i].join(",");

        const controller = host.scriptRoot.add(obj, lineKey)
        controller.onChange(function (value) {
            console.log(key, value)
            // host.save(key, value);
        });

        controller.onFinishChange(function (value) {
            const arr = value.split(",").map(item => {
                return item;
            });

            // const isValid = arr.every(item => {
            //     console.log(item);
            //     console.log(typeof item)
            //     return item === 0 || item === 1 ||  item === 2  || item === 3 || item === 4  || item === 5 || item === 6 || item === 7 || item === 8
            // })
            // if (isValid) {
            //     host.save(key, [arr]);
            // }
            // else {
            //     alert("非法数据")
            // }

            script[key][i] = arr;
            host.save(key, script[key]);


            // host.save(key, value);
        });

    }

}


export class TileMapRenderer extends RenderableBehaviour {


    @SerilizeField(propertyEditor_TileMap)
    tileData: number[][] = [];

    grid: astar.Grid;

    onStart() {
        this.displayObject.renderNode = this;
        this.grid = new astar.Grid(20, 20);

        for (let rowIndex = 0; rowIndex < this.tileData.length; rowIndex++) {
            const row = this.tileData[rowIndex];
            for (let colIndex = 0; colIndex < row.length; colIndex++) {
                const walkable = row[colIndex] == 1 ? false : true;
                this.grid.setWalkable(colIndex, rowIndex, walkable)
            }
        }

        // createPrefab from tileData

        for (let rowIndex = 0; rowIndex < this.tileData.length; rowIndex++) {
            const row = this.tileData[rowIndex];
            for (let colIndex = 0; colIndex < row.length; colIndex++) {
                const data = row[colIndex];
                const gameObjectPrefabData = createTileMapItemPrefab(data);
                const gameObject = core.createPrefab(gameObjectPrefabData);
                this.displayObject.addChild(gameObject);
                const transform = gameObject.getScript(Transform);
                transform.x = colIndex * 64;
                transform.y = rowIndex * 64;
            }
        }



    }
    onUpdate() {
    }

    onPropertyChanged(key: string, value: any) {
        if (key === "tileData") {
            this.displayObject.removeAllChildren();
            for (let rowIndex = 0; rowIndex < this.tileData.length; rowIndex++) {
                const row = this.tileData[rowIndex];
                for (let colIndex = 0; colIndex < row.length; colIndex++) {
                    const data = row[colIndex];
                    const gameObjectPrefabData = createTileMapItemPrefab(data);
                    const gameObject = core.createPrefab(gameObjectPrefabData);
                    this.displayObject.addChild(gameObject);
                    const transform = gameObject.getScript(Transform);
                    transform.x = colIndex * 64;
                    transform.y = rowIndex * 64;
                }
            }
        }
        console.log('属性改变', key, value)
    }

    onRender(context: CanvasRenderingContext2D) {

    }


    getRenderArea() {
        return {
            x: 0,
            y: 0,
            width: this.grid.numCols * 64,
            height: this.grid.numRows * 64
        }
    }
}